
import java.awt.*;
import java.awt.geom.Line2D;

public class Main extends java.applet.Applet implements Runnable {

    Image scrnBuf;
    Graphics scrnG;
    int frame;
    int delay;;
    Thread animator;

    /**
     * inisialisasi Applet dan tentukan FPS
     */
    @Override
    public void init() {        
        scrnBuf = createImage(800,600);
        scrnG = scrnBuf.getGraphics();
        setSize(800, 600);
        String str = getParameter("fps");
        int fps = (str != null) ? Integer.parseInt(str) : 10;
        delay = (fps > 0) ? (1000 / fps) : 100;
    }

    /**
     * Method ini dipanggil saat Applet mulai di render ke layar monitor
     * Buat Thread baru dan start()
     */
    @Override
    public void start() {
        animator = new Thread(this);
        animator.start();
    }

    /**
     * Method ini adalah yang dijalan ketika Thread di start()
     */
    public void run() {
        // Remember the starting time
        long tm = System.currentTimeMillis();
        while (Thread.currentThread() == animator) {
            // Display the next frame of animation.
            repaint();

            // Delay depending on how far we are behind.
            try {
                tm += delay;
                Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
            } catch (InterruptedException e) {
                break;
            }

            // Advance the frame
            frame++;
        }
    }

    /**
     * Method ini dipanggil ketika Applet telah di tutup
     */
    @Override
    public void stop() {
        animator = null;
    }

    /**
     * Method ini dipanggil untuk penggambaran grafis
     */
    @Override
    public void paint(Graphics g) {
        update(g);
    }

    /**
     * Digunakan untuk mengupdate kondisi
     */
    @Override
    public void update(Graphics g) {
       g.clipRect(getX(),getY(),this.getWidth(),this.getHeight());
       drawBackground(g);
    }

    public void drawBackground(Graphics g) {
        float dashes3[] = {40.0f};
	BasicStroke stroke = new BasicStroke(5.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER,1.0f, dashes3, 0.0f);
        
        scrnG.setColor(Color.blue);
        scrnG.fillRect(650, 0, 50, 250);   //IN A
        scrnG.fillRect(650, 350, 50, 250); //IN B
        scrnG.fillRect(100, 0, 50, 250);   //OUT A
        scrnG.fillRect(100, 350, 50, 250); //OUT B
//---------------------------------------------------------->>
        scrnG.setColor(Color.gray);
        scrnG.fillRect(50, 280, 700, 40); //tengah
        scrnG.fillRect(190, 40, 420, 40);  //atas
        scrnG.fillRect(190, 520, 420, 40);  //bawah
        scrnG.fillRect(30, 0, 40, 600);
        scrnG.fillRect(190, 40, 40, 500); //A (kiri)
        scrnG.fillRect(570, 40, 40, 500); //A (kanan)
        scrnG.fillRect(730, 0, 40, 600);
//---------------------------------------------------------->>
        scrnG.setColor(Color.RED);
        //horisontal
        scrnG.drawLine(250, 175, 550, 175); //A
        scrnG.drawLine(250, 425, 550, 425); //B
        //vertikal
        scrnG.drawLine(250, 100, 250, 250); //A
        scrnG.drawLine(300, 100, 300, 250);
        scrnG.drawLine(350, 100, 350, 250);
        scrnG.drawLine(400, 100, 400, 250);
        scrnG.drawLine(450, 100, 450, 250);
        scrnG.drawLine(500, 100, 500, 250);
        scrnG.drawLine(550, 100, 550, 250);
        scrnG.drawLine(250, 350, 250, 500); //B
        scrnG.drawLine(300, 350, 300, 500);
        scrnG.drawLine(350, 350, 350, 500);
        scrnG.drawLine(400, 350, 400, 500);
        scrnG.drawLine(450, 350, 450, 500);
        scrnG.drawLine(500, 350, 500, 500);
        scrnG.drawLine(550, 350, 550, 500);
//---------------------------------------------------------->>
        scrnG.setColor(Color.black);
        scrnG.setFont(new Font("arial", Font.PLAIN, 10));
        scrnG.drawString("6", 270, 220);
        scrnG.drawString("5", 320, 220);
        scrnG.drawString("4", 370, 220);
        scrnG.drawString("3", 420, 220);
        scrnG.drawString("2", 470, 220);
        scrnG.drawString("1", 520, 220);
        scrnG.drawString("7", 270, 140);
        scrnG.drawString("8", 320, 140);
        scrnG.drawString("9", 370, 140);
        scrnG.drawString("10", 420, 140);
        scrnG.drawString("11", 470, 140);
        scrnG.drawString("12", 520, 140);
        scrnG.drawString("13", 520, 390);
        scrnG.drawString("14", 470, 390);
        scrnG.drawString("15", 420, 390);
        scrnG.drawString("16", 370, 390);
        scrnG.drawString("17", 320, 390);
        scrnG.drawString("18", 270, 390);
        scrnG.drawString("19", 270, 470);
        scrnG.drawString("20", 320, 470);
        scrnG.drawString("21", 370, 470);
        scrnG.drawString("22", 420, 470);
        scrnG.drawString("23", 470, 470);
        scrnG.drawString("24", 520, 470);
//---------------------------------------------------------->>
        scrnG.setColor(Color.magenta);
        scrnG.setFont(new Font("arial", Font.PLAIN, 20));
        scrnG.drawString("in", 660, 270);  //IN
        scrnG.drawString("out", 120, 270); //OUT
//---------------------------------------------------------->>
        Graphics2D g2d = (Graphics2D) scrnG;
        scrnG.setColor(Color.white);
        // g2d.setStroke(stroke);

        g2d.draw(new Line2D.Double(50, 0, 50, 600));	//kiri luar
        g2d.draw(new Line2D.Double(210, 60, 210, 540)); //kiri dalam
        g2d.draw(new Line2D.Double(750, 0, 750, 600));	//kanan luar
        g2d.draw(new Line2D.Double(590, 60, 590, 540)); //kanan dalam

        g2d.draw(new Line2D.Double(210, 60, 600, 60));  //atas
        g2d.draw(new Line2D.Double(50, 300, 750, 300)); 	 //tengah
        g2d.draw(new Line2D.Double(210, 540, 600, 540)); //bawah
//---------------------------------------------------------->>
        scrnG.setColor(Color.green);
        scrnG.fillRect(690, 250, 10, 100); //PINTU IN
        scrnG.fillRect(100, 250, 10, 100); //PINTU OUT

        g.drawImage(scrnBuf, 0 , 0 , this);
    }
}
